﻿using Microsoft.AspNetCore.Http;
using WCS.Infrastructure.EFCore;

namespace WCS.Infrastructure.Tools
{
    public static class HttpContextExtensions
    {
        /// <summary>
        /// 从HttpContext中获取当前用户
        /// </summary>
        public static T? GetCurrentUser<T>(this HttpContext httpContext) where T : class
        {
            if (httpContext.Items.TryGetValue("CurrentUser", out var user) && user is T typedUser)
            {
                return typedUser;
            }
            return null;
        }

        /// <summary>
        /// 获取当前用户ID
        /// </summary>
        public static string? GetCurrentUserId(this HttpContext httpContext)
        {
            if (httpContext.Items.TryGetValue("CurrentUser", out var user))
            {
                if (user is SysUser sysUser)
                {
                    return sysUser.Id;
                }
                else if (user is SysUserMiniProgram miniUser)
                {
                    return miniUser.Id;
                }
            }
            return "";
        }
    }
}